import json

from dotmap import DotMap
from flask import request, jsonify


def return_fail(msg: str = "unexpect error"):
    return jsonify({
        'status': 0,
        'msg': msg or "unexcept error"
    })


def return_ok():
    return jsonify({
        'status': 1
    })


def return_simple_data(data):
    return jsonify({
        'status': 1,
        'data': data
    })


def gen_table_name(anm, ctype):
    return '%s_%s' % (anm, ctype)


def parser_req():
    """
    只有2中方式：GET POST, 如果为POST方式，必须是 json.loads(request.data)
    """
    print("method:", request.method)
    if request.method == 'GET':
        return DotMap(request.args)

    if request.method == 'POST':
        param = {}
        param.update(request.args)
        if request.form:
            t = request.form
            param.update(t)
        elif request.data:
            t = json.loads(request.data)
            param.update(t)
        print("param", param)
        return DotMap(param)
